package com.lanou.myim.model;

import android.text.TextUtils;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.myim.db.ApplyEty;
import com.lanou.myim.db.ApplyEtyDao;
import com.lanou.myim.presenter.OnCompletedListener;
import com.lanou.myim.util.CharacterParser;
import com.lanou.myim.util.PinyinComparator;
import com.lanou.myim.tools.DaoSingleton;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by dllo on 16/3/3.
 */
public class FriendsModel implements FriendsModelInterface{
    private List<String> userNames;

    private ApplyEtyDao applyEtyDao;

    private CharacterParser characterParser;
    private PinyinComparator pinyinComparator;


    public FriendsModel(){
        characterParser = CharacterParser.getInstance();
        pinyinComparator = new PinyinComparator();
        applyEtyDao = DaoSingleton.getInstance().getApplyEtyDao();
    }




    private List<SortModel> filterData(String filterStr,List<SortModel> SourceDateList) {
        List<SortModel> filterDateList = new ArrayList<>();

        if (TextUtils.isEmpty(filterStr)) {
            filterDateList = SourceDateList;
        } else {
            filterDateList.clear();
            for (SortModel sortModel : SourceDateList) {
                String name = sortModel.getName();
                if (name.indexOf(filterStr.toString()) != -1 || characterParser.getSelling(name).startsWith(filterStr.toString())) {
                    filterDateList.add(sortModel);
                }
            }
        }

        Collections.sort(filterDateList, pinyinComparator);
        return filterDateList;
    }



    private List<SortModel> filledData(List<String> userNames) {
        List<SortModel> mSortList = new ArrayList<>();
        for (int i = 0; i < userNames.size(); i++) {
            SortModel sortModel = new SortModel();
            sortModel.setName(userNames.get(i));
            String pinyin = characterParser.getSelling(userNames.get(i));
            String sortString = pinyin.substring(0, 1).toUpperCase();

            if (sortString.matches("[A-Z]")) {
                sortModel.setSortLetters(sortString.toUpperCase());
            } else {
                sortModel.setSortLetters("#");
            }

            mSortList.add(sortModel);
        }
        Collections.sort(mSortList, pinyinComparator);
        return mSortList;
    }

    @Override
    public List<SortModel> getFriendList() {
        EMChatManager.getInstance().getChatOptions().setUseRoster(true);
        try {
            userNames = EMContactManager.getInstance().getContactUserNames();
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
        return filledData(userNames);
    }

    @Override
    public List<SortModel> getFilterFriendList(String str) {
        return filterData(str,getFriendList());
    }

    @Override
    public void deleteFriend(String userName,OnCompletedListener listener) {
        try {
            EMContactManager.getInstance().deleteContact(userName);
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
        listener.onCompleted();
    }

    @Override
    public void addFriendToBlackList(String userName, Boolean flag,OnCompletedListener listener) {
        try {
            EMContactManager.getInstance().addUserToBlackList(userName,flag);
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
        listener.onCompleted();
    }

    @Override
    public void insertData(String name, String reason, boolean flag,OnCompletedListener listener) {
        ApplyEty applyEty = new ApplyEty(applyEtyDao.count(),EMChatManager.getInstance().getCurrentUser(),name,reason,flag);
        applyEtyDao.insert(applyEty);
        listener.onCompleted();
    }

    @Override
    public List<SortModel> getAddFriendList(List<String> names) {
        EMChatManager.getInstance().getChatOptions().setUseRoster(true);
        try {
            userNames = EMContactManager.getInstance().getContactUserNames();
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
        for (String name:names){
            boolean different = true;
            for (String userName:userNames){
                if (userName.equals(name)){
                    different =false;
                }
            }
            if (different){
                userNames.add(name);
            }
        }
        return filledData(userNames);
    }


}
